package huxibianjie.com.lbscandroid.ui.new_activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.base.BaseActivity;
import huxibianjie.com.lbscandroid.event.ClickEvent;
import huxibianjie.com.lbscandroid.event.LoginEvent;
import huxibianjie.com.lbscandroid.manager.RouteManager;
import huxibianjie.com.lbscandroid.manager.UserManager;
import huxibianjie.com.lbscandroid.model.UserModel;
import huxibianjie.com.lbscandroid.task.Callback;
import huxibianjie.com.lbscandroid.task.Task;
import huxibianjie.com.lbscandroid.ui.Service;

/**
 * 活动任务
 */
public class CampaignActivity extends BaseActivity implements View.OnClickListener {
    private TextView mTvEnergy;
    private RelativeLayout mTvInvitationBtn;
    private TextView mIvCommunityOff;
    private ImageView mIvCommunityOn;
    private RelativeLayout mTvCommunityBtn;
    private TextView mIvFollowOff;
    private ImageView mIvFollowOn;
    private RelativeLayout mTvFollowBtn;

    private UserModel userModel;

    private TextView tvInvitationInfo, tvCommunityInfo, tvFollowInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_campaign;
    }

    protected void initView() {
        initTitle(R.drawable.ic_back, "活动任务");
        mTvEnergy = findViewById(R.id.tv_energy);
        mTvInvitationBtn = findViewById(R.id.tv_invitation_btn);

        mIvCommunityOff = findViewById(R.id.iv_community_off);
        mIvCommunityOn = findViewById(R.id.iv_community_on);
        mTvCommunityBtn = findViewById(R.id.tv_community_btn);

        mIvFollowOff = findViewById(R.id.iv_follow_off);
        mIvFollowOn = findViewById(R.id.iv_follow_on);
        mTvFollowBtn = findViewById(R.id.tv_follow_btn);

        tvInvitationInfo = findViewById(R.id.tv_invitation_info);
        tvCommunityInfo = findViewById(R.id.tv_community_info);
        tvFollowInfo = findViewById(R.id.tv_follow_info);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void initData() {
        tvInvitationInfo.setText(Html.fromHtml("邀请一个好友最多可以获得<font color='#1EFF93'>8000</font>双倍能量"));
        tvCommunityInfo.setText(Html.fromHtml("加入社区奖励<font color='#1EFF93'>2500</font>双倍能量"));
        tvFollowInfo.setText(Html.fromHtml("关注公众号奖励<font color='#1EFF93'>1000</font>双倍能量"));

        userModel = UserManager.getInstance().getUserModel();
        if (userModel != null) {
            mTvEnergy.setText(String.valueOf("当前双倍能量：" + userModel.getGoldPool()));
            if (userModel.getCommunityTask() == 1) {
                mTvCommunityBtn.setClickable(false);
                mIvCommunityOff.setVisibility(View.GONE);
                mIvCommunityOn.setVisibility(View.VISIBLE);
            } else {
                mTvCommunityBtn.setClickable(true);
                mIvCommunityOff.setVisibility(View.VISIBLE);
                mIvCommunityOn.setVisibility(View.GONE);
            }

            if (userModel.getFollowWechat() == 1) {
                mTvFollowBtn.setClickable(false);
                mIvFollowOff.setVisibility(View.GONE);
                mIvFollowOn.setVisibility(View.VISIBLE);
            } else {
                mTvFollowBtn.setClickable(true);
                mIvFollowOff.setVisibility(View.VISIBLE);
                mIvFollowOn.setVisibility(View.GONE);
            }
        }
    }

    @Override
    protected void initListener() {
        mTvInvitationBtn.setOnClickListener(this);
        mTvCommunityBtn.setOnClickListener(this);
        mTvFollowBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_invitation_btn:
                RouteManager.getInstance().showInviteFriendActivity(this);
                break;
            case R.id.tv_community_btn:
                RouteManager.getInstance().showCommunityTask(this);
                break;
            case R.id.tv_follow_btn:
                RouteManager.getInstance().showFollowActivity(this);
                break;
        }
    }

    @Override
    public void onClickEvent(ClickEvent event) {
        super.onClickEvent(event);
        switch (event.type) {
            case REFRESH_USER_INFO:
                if (userModel != null) {
                    showLoading();
                    Task.create(Service.class).getUserInfo(userModel.getPhone()).enqueue(new Callback<UserModel>() {
                        @Override
                        public void onSuccess(@Nullable UserModel userModel) {
                            if (userModel != null) {
                                UserManager.getInstance().save(userModel, false);
                                initData();
                                EventBus.getDefault().post(new ClickEvent(ClickEvent.Type.REFRESH_USER_INFO_SUCCESS, null, ""));
                            }
                            cancelLoading();
                        }

                        @Override
                        public void onFailure(@NonNull String error) {
                            cancelLoading();
                        }
                    });
                }
                break;
        }
    }

    @Override
    public void onLoginEvent(LoginEvent event) {
        super.onLoginEvent(event);
        if (event.isLogin) {
            initData();
        }
    }
}
